#include "../main.h"

/* 
--------------------------------------------------------------------------------
	函数功能：PPPOE设置
--------------------------------------------------------------------------------
*/
int wan_set_pppoe(int sockfd, char *tagid, char *msg)
{
	int ret = 0;
	char data[1024];
	char username[128];
	char password[128];
	cJSON *pRow = NULL, *pRoot = NULL, *pValue = NULL;

	pRow = cJSON_CreateObject();

	pRoot = cJSON_Parse(msg);
	if (pRoot == NULL)
	{
		cJSON_AddNumberToObject(pRow, "code", 1);
		cJSON_AddStringToObject(pRow, "msg", "JSON解析失败!");
	}
	else
	{
		memset(username, 0, sizeof(username));
		pValue = cJSON_GetObjectItem(pRoot, "username");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(username, pValue->valuestring, sizeof(username));

		memset(password, 0, sizeof(password));
		pValue = cJSON_GetObjectItem(pRoot, "password");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(password, pValue->valuestring, sizeof(password));

		if (strlen(username) && strlen(password))
		{
			set_value("network.wan.proto", "pppoe");
			set_value("network.wan.username", username);
			set_value("network.wan.password", password);
			set_value("network.wan.dns", "");

			system("sleep 3 && /etc/init.d/network restart &");

			cJSON_AddNumberToObject(pRow, "code", 0);
			cJSON_AddStringToObject(pRow, "msg", "Success");
		}
		else
		{
			cJSON_AddNumberToObject(pRow, "code", 1);
			cJSON_AddStringToObject(pRow, "msg", "PPPOE设置,参数为空");
		}
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "COMMON_SUCCESS");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_PPPOE_SET");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：PPPOE获取
--------------------------------------------------------------------------------
*/
int wan_get_pppoe(int sockfd, char *tagid)
{
	int ret = 0;
	char value[128];
	char data[1024];
	cJSON *pRoot = NULL, *pRow = NULL;

	pRow = cJSON_CreateObject();

	//PPPOE username
	memset(value, 0, sizeof(value));
	ret = get_value("network.wan.username", &value);
	if (ret == 1 && strlen(value))
		cJSON_AddStringToObject(pRow, "username", value);

	//PPPOE password
	memset(value, 0, sizeof(value));
	ret = get_value("network.wan.password", &value);
	if (ret == 1 && strlen(value))
		cJSON_AddStringToObject(pRow, "password", value);

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "DEVICE_PPPOEGET_REP");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_PPPOEGET_REQ");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：静态IP设置
--------------------------------------------------------------------------------
*/
int wan_set_static(int sockfd, char *tagid, char *msg)
{
	int ret = 0;
	char dns[32];
	char dns1[16];
	char dns2[16];
	char ipaddr[16];
	char netmask[16];
	char gateway[16];
	char data[1024];
	cJSON *pRow = NULL, *pRoot = NULL, *pValue = NULL;

	pRow = cJSON_CreateObject();

	pRoot = cJSON_Parse(msg);
	if (pRoot == NULL)
	{
		cJSON_AddNumberToObject(pRow, "code", 1);
		cJSON_AddStringToObject(pRow, "msg", "JSON解析失败!");
	}
	else
	{
		memset(ipaddr, 0, sizeof(ipaddr));
		pValue = cJSON_GetObjectItem(pRoot, "ip");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(ipaddr, pValue->valuestring, sizeof(ipaddr));

		memset(netmask, 0, sizeof(netmask));
		pValue = cJSON_GetObjectItem(pRoot, "child");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(netmask, pValue->valuestring, sizeof(netmask));

		memset(gateway, 0, sizeof(gateway));
		pValue = cJSON_GetObjectItem(pRoot, "wlan");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(gateway, pValue->valuestring, sizeof(gateway));

		memset(dns1, 0, sizeof(dns1));
		pValue = cJSON_GetObjectItem(pRoot, "dns1");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(dns1, pValue->valuestring, sizeof(dns1));

		memset(dns2, 0, sizeof(dns2));
		pValue = cJSON_GetObjectItem(pRoot, "dns2");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(dns2, pValue->valuestring, sizeof(dns2));

		memset(dns, 0, sizeof(dns));
		if (strlen(dns1) && strlen(dns2))
			snprintf(dns, sizeof(dns), "%s %s", dns1, dns2);
		else if (strlen(dns1))
			memcpy(dns, dns1, sizeof(dns));
		else if (strlen(dns2))
			memcpy(dns, dns2, sizeof(dns));

		if (strlen(ipaddr) && strlen(netmask) && strlen(gateway) && strlen(dns))
		{
			set_value("network.wan.proto", "static");
			set_value("network.wan.ipaddr", ipaddr);
			set_value("network.wan.netmask", netmask);
			set_value("network.wan.gateway", gateway);
			set_value("network.wan.dns", dns);

			system("sleep 3 && /etc/init.d/network restart &");

			cJSON_AddNumberToObject(pRow, "code", 0);
			cJSON_AddStringToObject(pRow, "msg", "Success");
		}
		else
		{
			cJSON_AddNumberToObject(pRow, "code", 1);
			cJSON_AddStringToObject(pRow, "msg", "静态IP设置,参数为空");
		}
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "COMMON_SUCCESS");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_NET_STATIC_SET");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：静态IP获取
--------------------------------------------------------------------------------
*/
int wan_get_static(int sockfd, char *tagid)
{
	int ret = 0;
	char *ptr = NULL;
	cJSON *pRoot = NULL, *pRow = NULL;

	pRow = cJSON_CreateObject();

	char *ipaddr = get_value_change("network.wan.ipaddr");
	char *netmask = get_value_change("network.wan.netmask");
	char *gateway = get_value_change("network.wan.gateway");
	char *dns = get_value_change("network.wan.dns");

	if (char_has_value(ipaddr) > 0)
	{
		cJSON_AddStringToObject(pRow, "ip", ipaddr);
	}
	if (char_has_value(netmask) > 0)
	{
		cJSON_AddStringToObject(pRow, "child", netmask);
	}

	if (char_has_value(gateway) > 0)
	{
		cJSON_AddStringToObject(pRow, "wlan", gateway);
	}
	// //DNS
	if (char_has_value(dns) > 0)
	{
		if ((ptr = strchr(dns, ' ')) != NULL)
		{
			*ptr++ = 0;
			cJSON_AddStringToObject(pRow, "dns1", dns);
			cJSON_AddStringToObject(pRow, "dns2", ptr);
		}
		else
		{
			cJSON_AddStringToObject(pRow, "dns1", dns);
			cJSON_AddStringToObject(pRow, "dns2", "");
		}
	}
	else
	{
		cJSON_AddStringToObject(pRow, "dns1", "");
		cJSON_AddStringToObject(pRow, "dns2", "");
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "DEVICE_STATICGET_REP");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_STATICGET_REQ");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));

	delete_mem(ipaddr);
	delete_mem(netmask);
	delete_mem(gateway);
	delete_mem(dns);

	delete_mem(buffer);

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：动态IP设置
--------------------------------------------------------------------------------
*/
int wan_set_dhcp(int sockfd, char *tagid, char *msg)
{
	int ret = 0;
	int dType = 0;
	char dns[32];
	char dns1[16];
	char dns2[16];
	char data[1024];
	cJSON *pRow = NULL, *pRoot = NULL, *pValue = NULL;

	pRow = cJSON_CreateObject();

	pRoot = cJSON_Parse(msg);
	if (pRoot == NULL)
	{
		cJSON_AddNumberToObject(pRow, "code", 1);
		cJSON_AddStringToObject(pRow, "msg", "JSON解析失败!");
	}
	else
	{
		pValue = cJSON_GetObjectItem(pRoot, "dType");
		if (pValue != NULL && pValue->type == cJSON_Number)
			dType = pValue->valueint;

		memset(dns1, 0, sizeof(dns1));
		pValue = cJSON_GetObjectItem(pRoot, "dns1");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(dns1, pValue->valuestring, sizeof(dns1));

		memset(dns2, 0, sizeof(dns2));
		pValue = cJSON_GetObjectItem(pRoot, "dns2");
		if (pValue != NULL && pValue->type == cJSON_String)
			memcpy(dns2, pValue->valuestring, sizeof(dns2));

		memset(dns, 0, sizeof(dns));
		if (strlen(dns1) && strlen(dns2))
			snprintf(dns, sizeof(dns), "%s %s", dns1, dns2);
		else if (strlen(dns1))
			memcpy(dns, dns1, sizeof(dns));
		else if (strlen(dns2))
			memcpy(dns, dns2, sizeof(dns));

		if (dType == 1 && !strlen(dns))
		{
			cJSON_AddNumberToObject(pRow, "code", 1);
			cJSON_AddStringToObject(pRow, "msg", "动态IP设置,参数为空");
		}
		else
		{
			set_value("network.wan.proto", "dhcp");

			if (dType == 1)
				set_value("network.wan.dns", dns);
			else
				set_value("network.wan.dns", "");

			system("sleep 3 && /etc/init.d/network restart &");

			cJSON_AddNumberToObject(pRow, "code", 0);
			cJSON_AddStringToObject(pRow, "msg", "Success");
		}
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "COMMON_SUCCESS");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_NET_DYMIC_SET");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	free(buffer);
	buffer = NULL;

	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：动态IP获取
--------------------------------------------------------------------------------
*/
int wan_get_dhcp(int sockfd, char *tagid)
{
	int ret = 0;
	char *ptr = NULL;
	cJSON *pRoot = NULL, *pRow = NULL;
	pRow = cJSON_CreateObject();
	//DNS
	char *dns = get_value_change("network.wan.dns");
	if (char_has_value(dns) > 0)
	{
		cJSON_AddNumberToObject(pRow, "dType", 1);

		if ((ptr = strchr(dns, ' ')) != NULL)
		{
			*ptr++ = 0;
			cJSON_AddStringToObject(pRow, "dns1", dns);
			cJSON_AddStringToObject(pRow, "dns2", ptr);
		}
		else
		{
			cJSON_AddStringToObject(pRow, "dns1", dns);
			cJSON_AddStringToObject(pRow, "dns2", "");
		}
	}
	else
	{
		cJSON_AddNumberToObject(pRow, "dType", 0);
		cJSON_AddStringToObject(pRow, "dns1", "");
		cJSON_AddStringToObject(pRow, "dns2", "");
	}

	pRoot = cJSON_CreateObject();
	cJSON_AddStringToObject(pRoot, "type", "DEVICE_DYNAMICGET_REP");
	cJSON_AddStringToObject(pRoot, "replyType", "DEVICE_DYNAMICGET_REQ");
	cJSON_AddStringToObject(pRoot, "clientId", RouteID);
	cJSON_AddStringToObject(pRoot, "tagClientId", tagid);
	cJSON_AddStringToObject(pRoot, "content", cJSON_Print(pRow));

	char *buffer = cJSON_Print(pRoot);
	buffer[strlen(buffer)] = '\0';

	if (pRow != NULL)
		cJSON_Delete(pRow);

	if (pRoot != NULL)
		cJSON_Delete(pRoot);

	ret = send_msg(sockfd, buffer, strlen(buffer));
	delete_mem(dns);
	delete_mem(buffer);

	return ret;
}
